#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
int main()
{
    int result = -1;
    int fd[2],nbytes;
    pid_t pid;
    char str[] = "hello pipe";
    char readbuffer[80];
    int *write_fd = &fd[1];
    int *read_fd = &fd[0];
    result = pipe(fd);
    if(-1 == result)
    {
        cout<<"建立管道失败!"<<endl;
        return -1;
    }
    pid = fork();
    if(-1 == pid)
    {
        cout<<"创建子进程失败!"<<endl;
        return -1;
    }
    else if(0 == pid)
    {
        close(*read_fd);
        result = write(*write_fd,str,strlen(str));
        return 0;
    }
    else{
        close(*write_fd);
        nbytes = read(*read_fd,readbuffer, sizeof(readbuffer));
        cout<<"接受"<<nbytes<<"数据，数据为:"<<readbuffer<<endl;
        return 0;
    }
    return 0;


}